package com.maxatime.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maxatime.exception.PhoenixException;
import com.maxatime.pojo.Classroom;
import com.maxatime.pojo.ExamStorage;
import com.maxatime.service.ExamStorageService;
import com.maxatime.utils.MyContent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.Zhao
 * @title: ExamStorageController
 * @projectName phoenix
 * @description: TODO
 * @date 2021/8/4 8:33
 */
@Controller
public class ExamStorageController {
    Logger log= LogManager.getLogger();

    @Autowired
    private ExamStorageService examStorageService;

    @RequestMapping("/storageFind")
    @ResponseBody
    public Map<String,Object> storageFind(@RequestParam(required=false,defaultValue="1") int page,
                                            @RequestParam(required=false,defaultValue="7") int limit

    ){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
        List<Classroom> stu  = examStorageService.storageFind();// 这是我们的sql

        PageInfo<Classroom> rolePageInfo = new PageInfo<>(stu);//
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        log.info("map------------------------------------"+map);
        return map;
    }
    @RequestMapping("/insertOrg")
    @ResponseBody
    public Map<String,Object> insertOrg(@RequestBody String istDate){
        Map<String,Object> m=new HashMap<String,Object>();
        JSONObject jsonObject=JSONObject.parseObject(istDate);
        ExamStorage clas=new ExamStorage();
        clas.setGoodsName(jsonObject.getString("orgadd"));
        clas.setGoodsAmount(Integer.parseInt(jsonObject.getString("orgsadd")));
        clas.setGoodsType(jsonObject.getString("goodsType"));


        Calendar now = Calendar.getInstance();//获取时间
        clas.setCreateTime(now.getTime());//当前时间
        clas.setUpdateTime(now.getTime());
        try {

            Integer dd=  examStorageService.insertOrg(clas);

            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"添加成功");
            m.put("data",dd);

        }catch (PhoenixException e){
            e.printStackTrace();
            log.error("exception::"+e.getMessage());
            log.error("exception--code"+e.getCode());
            log.error("exception--msg"+e.getMsg());
            m.put(MyContent.CODE,e.getCode());
            m.put(MyContent.MSG,e.getMsg());
            m.put("data",0);

        }
        return m;


    }
    @RequestMapping("/orgsdelete")
    @ResponseBody
    public Integer orgsdelete(@RequestBody String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        String pk = jsonObject.getString("pkId");
        int pkId = Integer.parseInt(pk);


        return examStorageService.orgsdelete(pkId);
    }



}
